reports.js ➔ reportedit   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.95
c 0
b 0
f 0
cc 3
1
var express = require('express');
2
var router = express.Router();
3
const sqlite3 = require('sqlite3').verbose();
0 ignored issues
show
Unused Code introduced by
The constant sqlite3 seems to be never used. Consider removing it.
Loading history...
4
// const db = new sqlite3.Database('./db/texts.sqlite');
5
const db = require("../db/database.js");
6
const jwt = require('jsonwebtoken');
7
8
let config;
9
10
try {
11
    config = require('../config.js');
12
} catch (error) {
13
    console.error(error);
14
}
15
16
const jwtSecret = process.env.JWT_SECRET || config.secret;
17
18
router.post("/",
19
    (req, res, next) => checkToken(req, res, next),
20
    (req, res) => reportedit(res, req.body));
21
22
function checkToken(req, res, next) {
23
    const token = req.headers['x-access-token'];
24
25
    jwt.verify(token, jwtSecret, function(err, decoded) {
0 ignored issues
show
Unused Code introduced by
The parameter decoded is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
26
        if (err) {
27
            return res.status(401).json({
28
                data: {
29
                    message: "No valid token."
30
                }
31
            });
32
        }
33
        // Valid token send on the request
34
        next();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
35
    });
36
}
37
38
function reportedit(res, body) {
39
    const week = body.week;
40
    const text = body.text;
41
42
    db.run("UPDATE reports SET text = '" + text + "' WHERE week = " + week,
43
        (err) => {
44
            if (err) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
45
                // console.log(err);
46
            }
47
            return res.status(201).json({
48
                data: {
49
                    message: "Report successfully updated."
50
                }
51
            });
52
        });
53
}
54
55
module.exports = router;
56